#define _CRT_SECURE_NO_WARNINGS 1


#include<iostream>

using namespace std;

struct ListNode 
{
	int val;
	ListNode* next;
	ListNode() : val(0), next(nullptr) {}
	ListNode(int x) : val(x), next(nullptr) {}
	ListNode(int x, ListNode* next) : val(x), next(next) {}
};

int ListSize(ListNode* phead)
{
	ListNode* cur = phead;
	int size = 0;

	while (cur)
	{
		size++;
		cur = cur->next;
	}

	return size;
}

int kthToLast(ListNode* head, int k)
{
	int n = ListSize(head) - k;
	ListNode* cur = head;

	while (n--)
	{
		cur = cur->next;
	}

	return cur->val;
}

int main()
{
	ListNode* node1 = new ListNode(1);
	ListNode* node2 = new ListNode(2);
	ListNode* node3 = new ListNode(3);
	ListNode* node4 = new ListNode(4);
	ListNode* node5 = new ListNode(5);

	node1->next = node2;
	node2->next = node3;
	node3->next = node4;
	node4->next = node5;

	cout << kthToLast(node1, 2) << endl;

	return 0;
}